package android.itv.library;

import java.io.IOException;
import java.util.ArrayList;

import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.SharedPreferences;
import android.itv.modesevice.Channel;
import android.itv.util.AppConfi;
import android.preference.PreferenceManager;
import android.util.Log;

public class ServiceHelper {
    SharedPreferences sharedPrefere;

    public ServiceHelper(Activity activity) {
	sharedPrefere = PreferenceManager.getDefaultSharedPreferences(activity);
    }

    @SuppressWarnings("unchecked")
    public ArrayList<Channel> getAllChannel(String name, String pass) {
	ArrayList<Channel> temp = new ArrayList<Channel>();
	final String URL = "http://"
		+ sharedPrefere.getString("localhost", "192.168.137.1") + ":"
		+ sharedPrefere.getString("port", "8800") + AppConfi.URL;

	Log.d("URL:", URL);

	SoapObject request = new SoapObject(AppConfi.NAMESPACE,
		AppConfi.METHOD_NAME_LOGIN);
	// Khai bao tham so truyen vao cho phuong thuc

	PropertyInfo infoName = new PropertyInfo();

	infoName.setName("name");
	infoName.setValue(name);
	infoName.setType(PropertyInfo.STRING_CLASS);

	PropertyInfo infoPass = new PropertyInfo();
	infoPass.setName("pass");
	infoPass.setValue(pass);
	infoPass.setType(PropertyInfo.STRING_CLASS);

	// add them tham so vao phuong thuc
	request.addProperty(infoName);
	request.addProperty(infoPass);

	SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
		SoapSerializationEnvelope.VER11);
	envelope.dotNet = false;
	envelope.setOutputSoapObject(request);

	envelope.addMapping(AppConfi.NAMESPACE, "Channel",new Channel().getClass());
	HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,6000);

	Log.i("Sani", "coming 1");
	System.out.println("Kiem tra:"+AppConfi.SOAP_ACTION_LOGIN );
	System.out.println("Kiem tra:"+AppConfi.NAMESPACE );
	System.out.println("Kiem tra:"+AppConfi.METHOD_NAME_LOGIN );
	try {
	    Log.i("Sani", "coming 2");
	    androidHttpTransport.call(AppConfi.SOAP_ACTION_LOGIN, envelope);
	    Log.i("Sani", "coming 3");
	} catch (IOException e) {
	    Log.i("Sani chay vao day", "error" + e.getMessage());
	} catch (XmlPullParserException e) {
	    Log.i("Sani", "error" + e.getMessage());
	}
	
	SoapObject result = null;
	Log.i("Sani", "coming 4");
	result = (SoapObject) envelope.bodyIn; // I get the error

	Log.i("Sani", "coming 5");
	for (int i = 0; i < result.getPropertyCount(); i++) {

	    SoapObject temp2 = (SoapObject) result.getProperty(i);
	    Channel kenh = new Channel(
		    temp2.getProperty("category").toString(), temp2
			    .getProperty("descriptions").toString(), temp2
			    .getProperty("idChannel").toString(), temp2
			    .getProperty("linkLogo").toString(), temp2
			    .getProperty("linktv").toString(), temp2
			    .getProperty("name").toString());
	    temp.add(kenh);
	}
	System.out
		.println("Kiem tra ket qua name:" + result.getPropertyCount());
	// Kenh tempkenh= (Kenh) result.getProperty(0);
	System.out.println("Kiem tra ket qua: tenkenh"
		+ temp.get(0).getProperty(0));
	System.out.println("Kiem tra ket qua: thumbe"
		+ temp.get(0).getProperty(1));
	System.out.println("Kiem tra ket qua: ghichu"
		+ temp.get(0).getProperty(2));
	System.out.println("Kiem tra ket qua: idkenh"
		+ temp.get(0).getProperty(3));
	System.out.println("Kiem tra ket qua: link"
		+ temp.get(0).getProperty(4));
	// temp=(ArrayList<Kenh>) result.getProperty(0);
	return temp;
    }
    public String getSchedule(String idChannel,String date)
    {
	String temp="";
	final String URL = "http://"
		+ sharedPrefere.getString("localhost", "192.168.137.1") + ":"
		+ sharedPrefere.getString("port", "8800") + AppConfi.URL;

	Log.d("URL:", URL);

	SoapObject request = new SoapObject(AppConfi.NAMESPACE,
		AppConfi.METHOD_NAME_BROADCAST);
	// Khai bao tham so truyen vao cho phuong thuc

	PropertyInfo infoName = new PropertyInfo();

	infoName.setName("idChannel");
	infoName.setValue(idChannel);
	infoName.setType(PropertyInfo.STRING_CLASS);

	PropertyInfo infoPass = new PropertyInfo();
	infoPass.setName("date");
	infoPass.setValue(date);
	infoPass.setType(PropertyInfo.STRING_CLASS);

	// add them tham so vao phuong thuc
	request.addProperty(infoName);
	request.addProperty(infoPass);

	SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
		SoapSerializationEnvelope.VER11);
	envelope.dotNet = false;
	envelope.setOutputSoapObject(request);
	
	HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,6000);

	
	try {
	    Log.i("Sani", "coming 2");
	    androidHttpTransport.call(AppConfi.SOAP_ACTION_BROADCAST, envelope);
	    Log.i("Sani", "coming 3");
	} catch (IOException e) {
	    Log.i("Sani chay vao day", "error" + e.getMessage());
	} catch (XmlPullParserException e) {
	    Log.i("Sani", "error" + e.getMessage());
	}
	
	SoapObject result = null;
	Log.i("Sani", "coming 4");
	result = (SoapObject) envelope.bodyIn; // I get the error

		
	return result.getProperty(0).toString();
    }

}
